<?php

namespace frontend\controllers;

use Yii;
use frontend\models\UserinfoModel;

use frontend\models\UploadForm;
use yii\web\UploadedFile;
use db;



class ApplyController extends \yii\web\Controller
{
    //模拟登陆
    public $user_id = 1; 
    public $enableCsrfValidation = false; 
    public function actionIndex()
    {
        return $this->redirect(['info']);
    }
     //填写个人信息
    public function actionInfo()
    {
       $model = UserinfoModel::find()->where('user_id='.$this->user_id.' and status=1 order by userinfo_id desc');
       $model = $model->one();
       $modelUpload = new UploadForm;
 
       return $this->render('apply_info',['model'=>$model,'modelUpload'=>$modelUpload]);
    }
   
    public function actionResume()
    {
    	return $this->render('apply_resume');
    }

    public function actionReview()
    {
  
    	return $this->render('apply_review');
    }

    
    //上传信息 同时入库
    
    public function actionUserinfoadd()
    {   
       
       if (count($_FILES['UploadForm']['name']['imageFiles'])!=0) {
        $model = new UploadForm();
        $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
        $path = $this->actionUpload($model);
        $path = $path[0];
       }
        
       $mime = [
        'nickname'=>$_POST['nickname'],
            'phone'  =>$_POST['phone'],
            'img'    =>isset($path)?$path:'',
            'email'  =>$_POST['email'],
            'backup' =>$_POST['backup'],
            'brith'  =>$_POST['brith'],
            ];
        $mime = md5(implode(',',$mime));

       $sql = "UPDATE seven_userinfo set `STATUS`=0 where user_id = '".$this->user_id."'";
            Yii::$app->db->createCommand($sql)->execute();


        $status = Yii::$app->db->createCommand('SELECT userinfo_id FROM seven_userinfo where mime="'.$mime.'"')
            ->queryAll();
      if ($status) {
            
            $sql = "UPDATE seven_userinfo set `STATUS`=1 where mime = '".$mime."'";
            Yii::$app->db->createCommand($sql)->execute();
            $this->redirect(['resume']); return;
      }

        //dao 模式添加入库  
    
       $res =  Yii::$app->db->createCommand()->insert('seven_userinfo',[
           'user_id'=>$this->user_id,
    
            'nickname'=>$_POST['nickname'],
            'phone'  =>$_POST['phone'],
            'img'    =>isset($path)?$path:'',
            'email'  =>$_POST['email'],
            'backup' =>$_POST['backup'],
            'brith'  =>$_POST['brith'],
            'mime'   =>$mime,
            'sex'    =>$_POST['sex'],

            ])->execute();



        if ($res) {
           $this->redirect(['resume']);
        }

        
        

    }




      public function actionUpload($model)
      {

        
        if (Yii::$app->request->isPost) {
          
            if ($path = $model->upload()) {
                
                return $path;
            }else{
                
                return false;
            }
        }

          // return $this->render('upload', ['model' => $model]);
       }

}
